package one.bugu.android.demon.ui.widget;

import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import one.bugu.android.demon.R;
import one.bugu.android.demon.ui.refresh.util.DensityUtil;
import one.bugu.android.demon.util.ColorUtil;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: LangK
 * Created Date 2018/1/18
 * Time: 15:01
 * Description:
 */
public class TabScrollView extends HorizontalScrollView {


    private LinearLayout linearLayout;
    private Context context;

    private List<String> list;
    private int dafultColor = R.color.colorTextDefault;
    private int selectColor = R.color.colorOrange;
    private int textSize = 16;
    private static final int MARGIN_DISCEN_VERIVITY = DensityUtil.dp2px(10);
    private static final int MARGIN_DISCEN_HOR = DensityUtil.dp2px(15);

    private TabSelectListener tabSelectListener;

    public TabScrollView(Context context) {
        super(context);
        this.context = context;
        initView();
    }

    public TabScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        initView();
    }

    public TabScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initView();
    }


    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public TabScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }


    private void initView() {
        setHorizontalScrollBarEnabled(false);
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);

        linearLayout = new LinearLayout(context);
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        linearLayout.setBackgroundColor(ColorUtil.getColor(context, R.color.colorMain));
        linearLayout.setMinimumWidth(dm.widthPixels);

    }

    public void setSelect(int index) {
        if (list == null || list.isEmpty()) {
            throw new RuntimeException("list must init and size ge zero");
        }
        selectTab(index);
    }


    /**
     * 添加tab标签
     *
     * @param list
     */
    public TabScrollView addTabs(List<String> list) {
        this.list = list;
        return this;
    }

    /**
     * 设置tab标签字体大小
     *
     * @param size
     */
    public TabScrollView setTabTextSize(int size) {
        this.textSize = size;
        return this;
    }

    /**
     * 设置tab标签颜色
     *
     * @param color
     */
    public TabScrollView setTabDafultTextColor(int color) {
        this.dafultColor = color;
        return this;
    }

    /**
     * 设置tab标签颜色
     *
     * @param color
     */
    public TabScrollView setTabSelectTextColor(int color) {
        this.selectColor = color;
        return this;
    }

    public void build() {
        initList();
    }

    /**
     * 初始化tab标签
     */
    private void initList() {
        linearLayout.setWeightSum(list.size());
        for (int i = 0; i < list.size(); i++) {
            LinearLayout itemLayout = new LinearLayout(context);
            itemLayout.setGravity(Gravity.CENTER);
            itemLayout.setOrientation(LinearLayout.VERTICAL);
            TextView textView = new TextView(context);
            textView.setText(list.get(i));
            textView.setTextSize(textSize);
            textView.setTextColor(ColorUtil.getColor(context, dafultColor));
            textView.setGravity(Gravity.CENTER);
            LinearLayout.LayoutParams textLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            textLayoutParams.leftMargin = MARGIN_DISCEN_HOR;
            textLayoutParams.rightMargin = MARGIN_DISCEN_HOR;
            textLayoutParams.topMargin = MARGIN_DISCEN_VERIVITY;
            textLayoutParams.bottomMargin = MARGIN_DISCEN_VERIVITY;
            itemLayout.addView(textView, textLayoutParams);
            View view = new View(context);
            view.setVisibility(View.INVISIBLE);
            view.setBackgroundColor(ColorUtil.getColor(context, selectColor));
            LinearLayout.LayoutParams viewLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, DensityUtil.dp2px(2));
            itemLayout.addView(view, viewLayoutParams);
            itemLayout.setTag(i);
            itemLayout.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    selectTab((Integer) v.getTag());
                }
            });
            linearLayout.addView(itemLayout, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT,1));
        }
        addView(linearLayout, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
    }


    private void selectTab(int index) {
        for (int i = 0; i < linearLayout.getChildCount(); i++) {
            LinearLayout itemLayout = (LinearLayout) linearLayout.getChildAt(i);
            if (i == index) {
                ((TextView) itemLayout.getChildAt(0)).setTextColor(ColorUtil.getColor(context, selectColor));
                itemLayout.getChildAt(1).setVisibility(View.VISIBLE);
            } else {
                ((TextView) itemLayout.getChildAt(0)).setTextColor(ColorUtil.getColor(context, dafultColor));
                itemLayout.getChildAt(1).setVisibility(View.INVISIBLE);
            }
        }
        if (tabSelectListener != null) {
            tabSelectListener.onSelected(list.get(index));
        }
    }

    public void setOnTabSelectListener(TabSelectListener tabSelectListener) {
        this.tabSelectListener = tabSelectListener;
    }


    public interface TabSelectListener {
        void onSelected(String tag);
    }

}
